from flask import Blueprint, request, jsonify

import utils
from core.service import LanguageChangeService
from utils.error import ErrorHandler

chat_bp = Blueprint('chat', __name__)
service = LanguageChangeService()
error_handler = ErrorHandler()

@chat_bp.route('/translate', methods=['POST'])
@error_handler.catch_exceptions
def translate():
    data = request.get_json()
    text = data.get('text')
    target_language = data.get('target_language')
    translated_text = service.translate(text, target_language)
    return jsonify({'translated_text': translated_text})

@chat_bp.route('/converse', methods=['POST','GET'])
@error_handler.catch_exceptions
def converse():
    data = request.get_json()
    conversation_id = data.get('conversation_id')
    message = data.get('message')
    response = service.converse(conversation_id, message)
    return jsonify({'response': response})
